package com.mtsmda.java7features;

public class MultiCatchExceptions {

	public static void main(String[] args) {

		try {
			excep("first");
		} catch (FirstException | SecondException | ThirdException e) {
			System.out.println(e.getClass().getCanonicalName());
		}

		try {
			rethrow("abc");
		} catch (FirstException | SecondException | ThirdException e) {
			// below assignment will throw compile time exception since e is
			// final
			// e = new Exception();
			System.out.println(e.getMessage());
		}

	}

	static void rethrow(String s) throws FirstException, SecondException,
			ThirdException {
		try {
			if (s.equals("First"))
				throw new FirstException("First");
			else if (s.equals("Second"))
				throw new SecondException("Second");
			else
				throw new ThirdException("Third");
		} catch (Exception e) {
			// below assignment disables the improved rethrow exception type
			// checking feature of Java 7
			// e=new ThirdException();
			throw e;
		}
	}

	private static void excep(String ex) throws FirstException,
			SecondException, ThirdException {
		if (ex.equals("first")) {
			throw new FirstException(ex);
		} else if (ex.equals("second")) {
			throw new SecondException(ex);
		} else {
			throw new ThirdException(ex);
		}
	}

}

class FirstException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public FirstException(String msg) {
		super(msg);
	}
}

class SecondException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public SecondException(String msg) {
		super(msg);
	}
}

class ThirdException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ThirdException(String msg) {
		super(msg);
	}
}